{
  "nbformat": 4,
  "nbformat_minor": 5,
  "metadata": {},
  "cells": [
    {
      "metadata": {},
      "source": [
        "<td>\n",
        "   <a target=\"_blank\" href=\"https://labelbox.com\" ><img src=\"https://labelbox.com/blog/content/images/2021/02/logo-v4.svg\" width=190/></a>\n",
        "</td>"
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "<td>\n",
        "<a href=\"https://colab.research.google.com/github/Labelbox/labelbox-python/blob/master/examples/project_configuration/project_setup.ipynb\" target=\"_blank\"><img\n",
        "src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>\n",
        "</td>\n",
        "\n",
        "<td>\n",
        "<a href=\"https://github.com/Labelbox/labelbox-python/tree/master/examples/project_configuration/project_setup.ipynb\" target=\"_blank\"><img\n",
        "src=\"https://img.shields.io/badge/GitHub-100000?logo=github&logoColor=white\" alt=\"GitHub\"></a>\n",
        "</td>"
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "# Project Setup\n",
        "* This notebok describes how to create and configure a project\n",
        "* This is the same as creating a new project in the editor and going through all of the steps."
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "* When a user creates a project with client.create_project() the project is not ready for labeling.\n",
        "    * An ontology must be set\n",
        "    * A Batch must be created"
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "!pip install labelbox[data]"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "import labelbox as lb\n",
        "from labelbox.schema.quality_mode import QualityMode\n",
        "import uuid"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "# API Key and Client\n",
        "Provide a valid api key below in order to properly connect to the Labelbox Client."
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "# Add your api key\n",
        "API_KEY=\"\"\n",
        "client = lb.Client(api_key=API_KEY)"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "### Create Dataset\n",
        "* Create dataset and attach data\n",
        "* More details on attaching data can be found [here](https://github.com/Labelbox/labelbox-python/blob/master/examples/basics/data_rows.ipynb)"
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "dataset = client.create_dataset(name=\"project_setup_demo\")\n",
        "global_keys = [\"id1\", \"id2\", \"id3\", \"id4\"]\n",
        "## Example image\n",
        "uploads = []\n",
        "# Generate data rows\n",
        "for i in range(1,5):\n",
        "    uploads.append({\n",
        "        'row_data':  f\"https://storage.googleapis.com/labelbox-datasets/People_Clothing_Segmentation/jpeg_images/IMAGES/img_000{i}.jpeg\",\n",
        "        \"global_key\": global_keys[i-1]\n",
        "    })\n",
        "task = dataset.create_data_rows(uploads)\n",
        "task.wait_till_done()\n",
        "print(\"ERRORS: \" , task.errors)\n",
        "print(\"RESULT URL: \", task.result_url)"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "### Create Or Select an Ontology\n",
        "* Optionally create an ontology or select from an existing one.\n",
        "* More details on ontology management can be found [here](https://github.com/Labelbox/labelbox-python/blob/master/examples/basics/ontologies.ipynb)\n",
        "   "
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "# Create\n",
        "ontology_builder = lb.OntologyBuilder(\n",
        "    tools=[lb.Tool(name=\"cat\", tool=lb.Tool.Type.BBOX)])\n",
        "ontology = client.create_ontology(\"project-setup-demo-ontology\",\n",
        "                                  ontology_builder.asdict())\n",
        "# Select existing ontology\n",
        "#ontology = client.get_ontology(\"<ontology_id>\")\n",
        "#ontology = existing_project.ontology()"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "### Create Project and Setup the Editor\n",
        "* Setting up a project will add an ontology and will enable labeling to begin\n",
        "* Creating batches will add all data_rows belonging to the dataset to the queue."
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "\n",
        "batch_project = client.create_project(name=\"Project Setup Demo\",\n",
        "                                quality_mode=QualityMode.Consensus, # For benchmarks use quality_mode = QualityMode.Benchmark\n",
        "                                media_type=lb.MediaType.Image)\n",
        "\n",
        "\n",
        "batch_project.setup_editor(ontology)\n"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "# Add data to your projects "
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "\n",
        "## When creating a batch you can also setup the data rows priority\n",
        "batch = batch_project.create_batch(\n",
        "  \"batch-demo-4\", # Each batch in a project must have a unique name\n",
        "  global_keys=global_keys, # A list of data rows or data row ids\n",
        "  priority=5, # priority between 1(Highest) - 5(lowest)\n",
        "  consensus_settings={\n",
        "    \"number_of_labels\": 2, \n",
        "    \"coverage_percentage\": 1 \n",
        "  } \n",
        ")\n",
        "print(\"Batch: \", batch)"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "### Review"
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "# Note setup_complete will be None if it fails.\n",
        "print(batch_project.setup_complete)\n",
        "print(batch_project.ontology())\n",
        "print([ds.name for ds in batch_project.batches()])\n"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "print(f\"https://app.labelbox.com/projects/{batch_project.uid}\")"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {},
      "source": [
        "# Cleanup"
      ],
      "cell_type": "markdown"
    },
    {
      "metadata": {},
      "source": [
        "# batch_project.delete()\n",
        "# dataset_project.delete()\n",
        "# dataset.delete()"
      ],
      "cell_type": "code",
      "outputs": [],
      "execution_count": null
    }
  ]
}